package com.shuoyue.ycgk.ui.papergroups.special;

import android.view.View;
import android.widget.TextView;

import androidx.recyclerview.widget.LinearLayoutManager;
import androidx.recyclerview.widget.RecyclerView;

import com.chad.library.adapter.base.BaseQuickAdapter;
import com.scwang.smartrefresh.layout.SmartRefreshLayout;
import com.shuoyue.ycgk.R;
import com.shuoyue.ycgk.base.BaseMvpActivity;
import com.shuoyue.ycgk.entity.UserTestPaper;
import com.shuoyue.ycgk.entity.SpecialType;
import com.shuoyue.ycgk.ui.papergroups.SpecialQuestionContract;
import com.shuoyue.ycgk.ui.questionbank.practice.specialtraining.questionitem.SpeciaDoingActivity;
import com.shuoyue.ycgk.utils.SizeUtil;
import com.shuoyue.ycgk.utils.SpacesItemDecoration;

import java.util.ArrayList;
import java.util.List;

import butterknife.BindView;
import butterknife.OnClick;

/**
 * 专项训练列表
 */
public class SpecialListActivity extends BaseMvpActivity<SpecialQuestionContract.Presenter>
        implements SpecialQuestionContract.View {


    @BindView(R.id.page_title)
    TextView pageTitle;
    @BindView(R.id.recycler_view)
    RecyclerView recyclerView;
    @BindView(R.id.refreshlayout)
    SmartRefreshLayout refreshlayout;

    SpecialAdapter specialAdapter;
    List<SpecialType> specialTypes = new ArrayList<>();

    int moduleId, subjectId;
    /**
     * 专项适配器点击监听
     */
    BaseQuickAdapter.OnItemChildClickListener specialLis = (adapter, view, position) -> {
        SpecialType specialType = (SpecialType) adapter.getItem(position);

        if (moduleId == 47 || moduleId == 58) {
            mPresenter.createSpecialPager(specialType.getName(),
                    specialType.getSectionId(),
                    0,
                    0);
        } else {
            mPresenter.createSpecialPager(specialType.getName(),
                    specialType.getChapterId(),
                    specialType.getSectionId(),
                    specialType.getEpId());
        }
    };


    @Override
    protected int getLayoutId() {
        return R.layout.activity_special_list;
    }

    @Override
    protected void initView() {

        pageTitle.setText("专项训练");
        recyclerView.setLayoutManager(new LinearLayoutManager(mContext));
        recyclerView.addItemDecoration(new SpacesItemDecoration(SizeUtil.dip2px(mContext, 8), true));
        specialAdapter = new SpecialAdapter(specialTypes, specialLis);

        specialAdapter.setOnItemChildClickListener((adapter, view, position) -> {
            SpecialType specialType = (SpecialType) adapter.getItem(position);
            switch (view.getId()) {
                case R.id.lay_all:
                case R.id.start:
                    if (moduleId == 47|| moduleId == 58) {
                        specialType.setSelect(!specialType.isSelect());
                        adapter.notifyDataSetChanged();
                    } else {
                        mPresenter.createSpecialPager(specialType.getName(),
                                specialType.getChapterId(),
                                specialType.getSectionId(),
                                specialType.getEpId());
                    }
                    break;
                case R.id.indicator:
                    specialType.setSelect(!specialType.isSelect());
                    adapter.notifyDataSetChanged();
                    break;
            }
        });

        recyclerView.setAdapter(specialAdapter);
        refreshlayout.setEnableLoadMore(false);
        refreshlayout.setOnRefreshListener(refreshLayout -> {
            mPresenter.getSpecialList(moduleId, subjectId);
            refreshLayout.finishRefresh(2000);
        });
        registEmptyView(specialAdapter);

    }

    @Override
    public void initData() {
        subjectId = getIntent().getIntExtra("subjectId", -1);
        moduleId = getIntent().getIntExtra("moduleId", -1);
        mPresenter = new SpecialQuestionContract.Presenter();
        mPresenter.attachView(this);
//        mPresenter.getSpecialList(moduleId, subjectId);
    }

    @OnClick(R.id.back)
    public void onViewClicked() {
        finish();
    }

    @Override
    public void setTypeList(List<SpecialType> data) {


        for (SpecialType specialTypeC : data) {
            specialTypeC.setChapterId(specialTypeC.getId());
            if (specialTypeC.getChildren() != null)
                for (SpecialType specialTypes : specialTypeC.getChildren()) {
                    specialTypes.setChapterId(specialTypeC.getId());
                    specialTypes.setSectionId(specialTypes.getId());
                    if (specialTypes.getChildren() != null)
                        for (SpecialType specialTypee : specialTypes.getChildren()) {
                            specialTypee.setChapterId(specialTypeC.getId());
                            specialTypee.setSectionId(specialTypes.getId());
                            specialTypee.setEpId(specialTypee.getId());

                        }
                }
        }
        specialAdapter.resetData(data);
    }

    @Override
    public void setPagerInfo(String name, UserTestPaper specialPager) {
//        startActivity(new Intent(mContext, QuestionSpecialActivityWrong.class)
//                .putExtra("pagerId", specialPager.getMemberTestPaperId()));
//        QuestionSpecialActivity.start(mContext, specialPager, name, null);
        SpeciaDoingActivity.start(mContext, specialPager, name, null);

    }

    @Override
    protected void onResume() {
        super.onResume();
        mPresenter.getSpecialList(moduleId, subjectId);
    }
}
